require 'lib'
require 'gadgets/radio_group'

module EventEditor
  module Commands
    class Condition
     def self.show(base, model, edit_command = [], options = {}, &func)
         if options[:static]
          @dynamic = false
         else
          @dynamic = true
         end
        @dialog = Gtk::Dialog.new("Conditions",
               base,
               Gtk::Dialog::MODAL,
               ["OK", Gtk::Dialog::RESPONSE_OK],
               ["Cancel", Gtk::Dialog::RESPONSE_CANCEL]
               )
        @dialog.window_position = Gtk::Window::POS_MOUSE
        @func = func
        @model = model

        @buttons = []
        @group = nil
        # init components
        @dialog.vbox.add(self.create_panel)
        @dialog.vbox.show_all
        @dialog.set_size_request(300, 450)

        self.load(edit_command)
        
        # run
        response = @dialog.run
        if response == Gtk::Dialog::RESPONSE_OK
          command = @dynamic ? self.command : self.static_command
          func.call(command) if command
        end
        @dialog.destroy
      end

      def self.create_panel(&func)
        main_box = Gtk::VBox.new
        notebook = Gtk::Notebook.new
        
        box = Gtk::VBox.new(true, 2)
        box.set_border_width(5)
        
        @condition_group = ::Gadgets::RadioGroup.new(:vertical)
        
        Dir::glob($res.tool_path + "event_editor/conditions/*").each do |path|
          if File.file?(path)
            n = File.basename(path).split(".")[0]
            t = Lib.fileNameToClass('event_editor/conditions/','EventEditor::Conditions::', n)
            @condition_group.add_element(n, n, t.new(@dialog, @model), :frame)
          end
        end

        notebook.append_page(@condition_group, Gtk::Label.new("1"))
        main_box.add(notebook)
        if @dynamic
          @else_check = Gtk::CheckButton.new('Show else statement')
          main_box.pack_start(@else_check, false ,false)
        end
        return main_box
      end

      def self.load(edit_command)
        unless edit_command.empty?
          @else_check.sensitive = false
          @else_check.active = edit_command[1]
          self.select_condition(edit_command[2][0])
          t = Lib.fileNameToClass('event_editor/conditions/','EventEditor::Conditions::', edit_command[2][0])

          t.load(edit_command[2])
        end
      end
      
      def self.static_command
        t = @condition_group.selected_component
        if t
          return t.commands
        else
          return nil
        end
      end
      
      def self.command
        t = self.static_command
        if t
          return 'if', @else_check.active? , self.static_command
        else
          return nil
        end
      end
      
      def self.command_to_s(model, command, margin = "")
        s1 = ""
        s2 = ""
        t = ""
        cmd = command
        if command[0] == 'if'
          s1 = '<span color="blue"><b>If:</b> '
          s2 = '</span>'
          cmd = command[2]
        end

        t = Lib.fileNameToClass('event_editor/conditions/','EventEditor::Conditions::', cmd[0]).command_to_s(model, cmd)

        return s1 + t + s2  
      end
      
      def self.select_condition(condition_command)
        @condition_group.selected_key=condition_command
      end
    end
  end
end